package wzh000;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.*;

public class MyPlayer {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		MyPlayer myPlayer = new MyPlayer();
		int port;
		String nickName = null;
		if (args.length < 1) {
			System.out.println("Usage: MyPlayer, nickName, [port]");
			return;
		}
		if (args.length == 1) {
			port = 36000;
		} else {
			nickName = args[0];
			String portStr = args[1];
			try {
				port = Integer.parseInt(portStr);
			} catch (NumberFormatException nfe) {
				System.out
						.println("invalid port number. Will default to 36000");
				port = 36000;
			}
		}
		Player me = new Player(port, nickName);
		// ListenFromTheRight listen = new ListenFromTheRight(port, me);
		// Thread rightThread = new Thread(listen);
		// rightThread.start();

		me.joinGame();

		if (me.isFirst() == true) {

			System.out.println("I'm the first player");
			Card card = me.drawCard();
			if (card == null) {
				System.out
						.print("Error in Drawing Card...Game exiting...Please try later");
				System.exit(1);
			}

			Card pairCard = me.checkPair(card); // if there's a pair, discard
												// the pair.
			if (pairCard == null) { // means we should add this card to the card
									// list
				me.addCard(card);
			} else {
				// when I get the last card, check if I have no card
				if ((me.haveNoCard() == true) && (card.isLastCard() == true)) {
					// send "out of cards"
					me.notifyNoCards();
				} else {
					me.discardPairCards(card, pairCard);
				}
			}

			me.sendYourTurnToLeftPlayer();

		}

		while (true) {
			String reply;
			// wait to receive
			String cmd = me.waitForRightPlayer();

			Gson gson = new Gson();
			OfferHandRequest req = gson.fromJson(cmd, OfferHandRequest.class);

			if (req.cmd.equals("your_turn")) {
				me.sendToRightPlayer("ok");

				Card card = me.drawCard();
				Card pairCard = me.checkPair(card); // if there's a pair,
													// discard
													// the pair.
				if (pairCard == null) { // means we should add this card to
										// the card
										// list
					me.addCard(card);
				}
				// else {
				me.discardPairCards(card, pairCard);
				// }

				if ((card.isLastCard() == true)) {
					// offer hand to my left
					System.out.print("I am sending offer to my left");
					me.sendOfferHandToLeftPlayer();
					continue;
				}

				me.sendYourTurnToLeftPlayer();

			} else if (req.cmd.equals("offer")) {

				// if I received numOfCardLeft = 0, it means that the right
				// player is out.
				// I have to reply with "ok", but don't send pickcard to the
				// right.
				// Then send offerhand to my left player
				if (req.numOfCardLeft == 0) {
					me.setRightPlayerOut();
					reply ="ok";

				} else {

					if (me.haveNoCard() == true)
						reply = "out";
					else
						reply = "ok";
				}
				me.sendToRightPlayer(reply);
				if (reply.equals("ok")) {
					if(me.getRightPlayerOut() == false){
						me.sendPickCardToRightPlayer(req.numOfCardLeft);
					}
					me.sendOfferHandToLeftPlayer();
				}

			} else if (req.cmd.equals("pick")) {

				// me.recievedPickCard(cmd);

			}

		}

	}

}
